//94.二叉树的中序遍历
//给你二叉树的根节点 root ，返回它节点值的中序遍历。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

//递归实现

class Solution {
public:
    void _inorderTraversal(TreeNode* root, vector<int>& v)
    {
        if (!root)
        {
            return;
        }
        _inorderTraversal(root->left, v);
        v.push_back(root->val);
        _inorderTraversal(root->right, v);

    }
    vector<int> inorderTraversal(TreeNode* root)
    {
        vector<int> v;
        _inorderTraversal(root, v);
        return v;
    }
};

//非递归实现
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root)
    {
        vector<int> v;
        stack<TreeNode*> s;
        TreeNode* cur = root;
        while (cur)
        {
            s.push(cur);
            cur = cur->left;
        }
        while (!s.empty() || cur)
        {
            cur = s.top()->right;
            v.push_back(s.top()->val);
            s.pop();
            while (cur)
            {
                s.push(cur);
                cur = cur->left;
            }
        }
        return v;
    }
};